/*************************************************************
 *******************     WBContacts    ***********************
 *******************      Ver. 1.0     ***********************
 *************************************************************

 Written By: Michael Lintz
 			 LIQUA Tech Services

 Site: http://liqua.com
 Email: mike@liqua.com

 Date: March 3, 2003

 Descritpion: The WBCalendar is a web based calendar,
 written in JAVA, using a mySQL backend.

***************************************************************
 NOTE! WBCalendar is free and should be used at your own risk.
 	   If you find this useful, please take a moment to send me
 	   an email letting me know.  I would be interested in your
 	   feedback, comments, suggestions, questions, and where you
 	   are using this calendar.  Also, if you do find this useful,
 	   I would greatly appreciate a donation via PayPal.com, sent
 	   to mike@liqua.com   --Thanks!

***************************************************************

 Java file compiled using the following command-

 On Win32 Environment (must include double quotes):
 javac -classpath ".;path to servlet.jar" WBCalendar.java
 Example:
 javac -classpath ".;c:\tomcat\lib\servlet.jar" WBCalendar.java

 On Linux Environment:
 javac -classpath /path/to/servlet.jar WBCalendar.java
 Example:
 javac -classpath /var/tomcat/lib/servlet.jar WBCalendar.java

****************************************************************

 The following table should be created in a mySQL database named
 calendar, if created in a different database name, be sure to
 change String wbcalendarDB.

 mySQL Dump of Table Structure:

  	CREATE TABLE wbcalendar (
  	EventID int(11) NOT NULL auto_increment,
  	EntryDate date default NULL,
	Description text,
	Notes text,
	EventName varchar(15) default NULL,
	PRIMARY KEY  (EventID)
	) TYPE=MyISAM;



****************************************************************/

import java.io.*;
import java.sql.*;
import java.util.Calendar;
import java.util.*;
import java.util.Date;
import java.text.*;
import java.lang.Integer;
import javax.servlet.http.*;
import javax.servlet.*;

public class testCalendar extends HttpServlet
{

	//**************** User Specific Changes *******************/
	public String wbcalendarDB = "jdbc:mysql://localhost:3306/calendar"; // name and location of the mySQL database file containing the wbcalendar table
  	public String mysqlUser = "root"; //mySQL User ID
  	public String mysqlPassword = "test"; //mySQL User Password
   	public String titleBar = "testCalendar"; // label shown on the top bar of the Calendar
   	public String pathURL = "http://localhost/servlet/testCalendarServlet";
   	public String mySqlJdbcDriver = "org.gjt.mm.mysql.Driver"; // location of jdbc driver in the classpath
   	//**********************************************************/

   	public SimpleDateFormat dfDateField = new SimpleDateFormat("M/d/yy");
   	public SimpleDateFormat monthName = new SimpleDateFormat("MMMM");       // display month for title and next/last month navigation
  	public SimpleDateFormat year = new SimpleDateFormat("yyyy");			// display year
	public SimpleDateFormat month = new SimpleDateFormat("M");
	public SimpleDateFormat day = new SimpleDateFormat("d");
	public SimpleDateFormat dateFormat = new SimpleDateFormat("MM/d/yy");
	public SimpleDateFormat dfMySQLDate = new SimpleDateFormat("yyyy-MM-dd"); // matches format used by MySQL database
  	public SimpleDateFormat dfDayOfMonth = new SimpleDateFormat("d");         // used for writing date numerals in the calendar



	//Entry Point

    public void doGet(HttpServletRequest req,HttpServletResponse res)
    	throws ServletException, IOException
    {

    	res.setContentType("text/html");
    	PrintWriter out = res.getWriter();
    	pathURL = req.getServletPath();

    	Calendar thisMonth = Calendar.getInstance();
    	java.util.Date thisDate = new java.util.Date();



    	//** look for an input date request (default = today)
    	if (req.getParameter("date") != null)
    	{
        	try
      		{
        		thisDate = dfDateField.parse(req.getParameter("date"));
        		thisMonth.setTime(thisDate);
      		}
      		catch (ParseException e)
      		{
        		out.println("Can not parse date. Using date=" + dfDateField.format(thisMonth.getTime()) + "<br>");
      		}
    	}

    	//** set the calendar to the first day of the month
    	thisMonth.set(Calendar.DAY_OF_MONTH,1);

    	//** select the correct type of output and send it:
    	out.println(htmlHeader());

    	if (req.getParameter("form") == null)
    	{
      		out.println(tableHeader(thisMonth));
      		out.println(tableCells(thisMonth));

    	}
      	else if (req.getParameter("form").equals("new"))
      	{
        	out.println(newEvent(req.getParameter("value")));
      	}


      	else if (req.getParameter("form").equals("list"))
      	{
        	out.println(listEvent(req.getParameter("value")));
      	}
      	else if (req.getParameter("form").equals("modify"))
      	{
        	out.println(modifyEvent(req.getParameter("value")));
      	}



  	}



  	String htmlHeader()
  	{
    	return "<HTML>"
     	+ "<head>"
     	+ "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>"
     	+ "<meta name='Author' content='Michael Lintz'>"
     	+ "<meta http-equiv='Pragma' content='no-cache'>"
		+ "<meta http-equiv='Cache-Control' content='no-cache'>"
     	+ "<title>testCalendar</title>"
     	+ "</head>";
  	}




  	String tableHeader(Calendar thisMonth)
  	{
    	Calendar nextMonth = (Calendar)thisMonth.clone(); nextMonth.add(Calendar.MONTH,+1);
    	Calendar lastMonth = (Calendar)thisMonth.clone(); lastMonth.add(Calendar.MONTH,-1);
    	return "<body link=#000000 vlink=#000000 alink=#000000>"
    	+ "<CENTER><TABLE BORDER=1 CELLSPACING=0 CELLPADDING=0 WIDTH=600>"
      	+ "<TR><TD BGCOLOR=#ffffff ALIGN=CENTER COLSPAN=7>"
      	+ "<TABLE WIDTH=600 BORDER=0 CELLSPACING=0 CELLPADDING=0>"


      	+ "<TR ALIGN=CENTER>"
        + "<TD width=100 valign=middle><A HREF=" + pathURL + "?date=" + dfDateField.format(lastMonth.getTime()) + "><b><FONT FACE=VERDANA SIZE=-1 COLOR=#000000>Back to<br></FONT><FONT FACE=VERDANA SIZE=-1 COLOR=#000000>" + monthName.format(lastMonth.getTime())

        + "</FONT></b></A></TD>"

        + "<TD width=400 valign=top><FONT FACE=VERDANA SIZE=+1 COLOR=#000000><B>" + titleBar

        + "<BR>"
        + monthName.format(thisMonth.getTime()) + " " + year.format(thisMonth.getTime()) + "</B><br>"
        + "<A HREF=" + pathURL + "><FONT SIZE=1 COLOR=#000000><b>Current Month</A> &#8226; "
        + "<a href=# onClick=window.print();return false>Print This Calendar</a></b></FONT><BR>"

        + "<BR></TD>"

        + "<TD width=100 valign=middle><A HREF=" + pathURL + "?date=" + dfDateField.format(nextMonth.getTime()) + "><b><FONT FACE=VERDANA SIZE=-1 COLOR=#000000>Go to<br>" + monthName.format(nextMonth.getTime()) + "</FONT></b></A></TD>"

        + "</TR></TABLE>"






     	+ "  </TD>"
     	+ "  </TR>"
     	+ "  <TR ALIGN=CENTER BGCOLOR=#ffffff>"
     	+ "  <TD WIDTH=14%><FONT FACE=VERDANA SIZE=1 COLOR=#000000><B>Sunday</B></FONT></TD>"
     	+ "  <TD WIDTH=14%><FONT FACE=VERDANA SIZE=1 COLOR=#000000><B>Monday</B></FONT></TD>"
     	+ "  <TD WIDTH=14%><FONT FACE=VERDANA SIZE=1 COLOR=#000000><B>Tuesday</B></FONT></TD>"
     	+ "  <TD WIDTH=14%><FONT FACE=VERDANA SIZE=1 COLOR=#000000><B>Wednesday</B></FONT></TD>"
     	+ "  <TD WIDTH=14%><FONT FACE=VERDANA SIZE=1 COLOR=#000000><B>Thursday</B></FONT></TD>"
     	+ "  <TD WIDTH=14%><FONT FACE=VERDANA SIZE=1 COLOR=#000000><B>Friday</B></FONT></TD>"
     	+ "  <TD WIDTH=14%><FONT FACE=VERDANA SIZE=1 COLOR=#000000><B>Saturday</B></FONT></TD>"
     	+ "  </TR>";
  	}




  	String tableCells(Calendar thisMonth)
  	{
    	StringBuffer returnValue = new StringBuffer();

    	//** record the current value of the field Calendar.MONTH
    	int iThisMonth = thisMonth.get(Calendar.MONTH);
    	boolean firstCell = true;

    	Calendar today = Calendar.getInstance(); //used to highlight today's date cell in the calendar
    	Calendar endOfMonth = (Calendar)thisMonth.clone();
    	endOfMonth.set(Calendar.DATE,endOfMonth.getActualMaximum(Calendar.DATE));
    	endOfMonth.add(Calendar.DATE,7-endOfMonth.get(Calendar.DAY_OF_WEEK));

    	//** set calendar to first day of the first week (may move back 1 month)
    	thisMonth.add(Calendar.DATE,1-thisMonth.get(Calendar.DAY_OF_WEEK));

    	//** get a recordset of this month's events, including extra days at beginning and end
    	String startDate = dfMySQLDate.format(thisMonth.getTime());

    	ResultSet rsEvents = null;

    	try
    	{
      		Class.forName(mySqlJdbcDriver).newInstance();
      		Connection conn = DriverManager.getConnection(wbcalendarDB,mysqlUser,mysqlPassword);
      		Statement stmt = conn.createStatement();


      		String sqlQueryString = "SELECT EventID, EntryDate, Description, SUBSTRING_INDEX(Notes, ' ', 2), EventName FROM wbcalendar "
       		+ "WHERE TO_DAYS(EntryDate) >= TO_DAYS('" + startDate + "')"
       		+ " ORDER BY EntryDate";
      		rsEvents = stmt.executeQuery(sqlQueryString);
      		if (!rsEvents.next()) rsEvents = null;     // no events in the whole month's display
    	}
    	catch(Exception e)
    	{
      		return e.getMessage();
    	}

    	do
    	{
      		returnValue.append("<TR BORDER=1 VALIGN=TOP>");
      		for (int i=0; i < 7; i++)
      		{
        		returnValue.append("<TD HEIGHT=100 WIDTH=\"14%\"");
        		if (thisMonth.get(Calendar.MONTH) != iThisMonth) returnValue.append(" BGCOLOR=#e0e0e0");  // dark background for out-of-month days
        		else if (dfDateField.format(thisMonth.getTime()).equals(dfDateField.format(today.getTime()))) returnValue.append(" BGCOLOR=#ffff00"); // grey background for today's date
        		else returnValue.append(" BGCOLOR=#ffffff");  // default white background

        		returnValue.append("><A HREF=" + pathURL + "?form=list&value=" + dfDateField.format(thisMonth.getTime()) + "><FONT FACE=VERDANA COLOR=#000000 SIZE=1><B>" + dfDayOfMonth.format(thisMonth.getTime()) + "</B></A>");


        		returnValue.append(todaysEvents(rsEvents,dfMySQLDate.format(thisMonth.getTime()))); // write out the cell contents

        		returnValue.append("</TD>");
        		thisMonth.add(Calendar.DATE,1);
      		}
      			returnValue.append("</TR>");
    	}
    	while(thisMonth.getTime().before(endOfMonth.getTime()));

    	//** restore date to first of this month:
    	thisMonth.add(Calendar.MONTH,-1);
    	thisMonth.set(Calendar.DATE,1);

    	return returnValue.toString();
  	}




  	String todaysEvents(ResultSet rsEvents, String today)
  	{
    	try
    	{
      		if (rsEvents==null || rsEvents.isAfterLast()) return "";   // no more events this month or empty ResultSet
      		StringBuffer returnValue = new StringBuffer("<font FACE=VERDANA size=1>");
      		while (dfMySQLDate.format(rsEvents.getDate("entrydate")).equals(today))
      		{
        		java.util.Date start = rsEvents.getDate("entrydate");

        		String eventNameSpan = rsEvents.getString("EventName");

        		returnValue.append("<br><A HREF=" + pathURL + "?form=modify&value=" + rsEvents.getString("EventID") + "><FONT FACE=VERDANA COLOR=#0000FF>" + eventNameSpan + "</font></A>&nbsp;");


 				if (rsEvents.getString("SUBSTRING_INDEX(Notes, ' ', 2)").equals(""))
        		{
        			returnValue.append(rsEvents.getString("Description"));
        		}
        		else
        		{
        			returnValue.append(rsEvents.getString("SUBSTRING_INDEX(Notes, ' ', 2)"));
        			returnValue.append("...");
        		}


        		if(!rsEvents.next()) return returnValue.append("</font>").toString(); // encountered end of events
      		}
      		return returnValue.append("</font>").toString();
    	}
    	catch (SQLException e)
    	{
      	return e.getMessage();
    	}
  	}

 /***************************/
 /* List Events Entry Point */
 /***************************/


	String listEvent(String date)
  	{


    	String description = null;
    	String eventid = null;
    	String notes = null;
    	String eventname = null;
    	String table = "" ;
    	int j = 0;
    	int recordCounter = 0;


    	String sqlQueryString ;
      	ResultSet rs ;


    	// retrieve the existing data for the selected event
    	try
    	{
      		Class.forName(mySqlJdbcDriver).newInstance();
      		Connection conn = DriverManager.getConnection(wbcalendarDB,mysqlUser,mysqlPassword);
      		Statement stmt = conn.createStatement();



      		SimpleDateFormat sdfInput = new SimpleDateFormat( "M/dd/yy" );
			SimpleDateFormat sdfOutput = new SimpleDateFormat ( "EEEE, MMMM dd, yyyy" );
    		String textDate = date;

 			Date newDate = sdfInput.parse( textDate );




      		sqlQueryString = "select *, DATE_FORMAT(EntryDate, '%c/%e/%y') from wbcalendar where DATE_FORMAT(EntryDate, '%c/%e/%y') ='" + date + "'";


      		rs = stmt.executeQuery(sqlQueryString);





      		table = "<table width=\"600\" border=1 align=\"center\" cellpadding=\"0\" cellspacing=\"0\">\n";
  			table += "<tr><td><div align=\"center\"><b><font face=Verdana>Schedule for "+ sdfOutput.format( newDate ) +"</font></b><br>\n";
  			table += "<center><font face=verdana size=1><a href=" + pathURL + "?form=new&value="+ date +"><b>New Event</a> &#8226;\n";
  			table += "<a href=" + pathURL + "?date=" + date + ">Calendar View</a> &#8226;\n";
  			table += " <a href=# onClick=window.print();return false>Print This Page</a></b><br><br></font></center></div></td></tr>\n";




      		j = 0;
      		recordCounter = 0;

      		while(rs.next())
      		{

      			j++;
      			if( j == 1 )
      			{



      				table += "<tr><td><table width=600 border=0 cellspacing=0 cellpadding=5>\n";
           			table += "<tr align=left valign=top>\n";
          			table += "<td width=143><b><font size=2 face=Verdana>Name</font></b></td>\n";
          			table += "<td width=123><b><font size=2 face=Verdana>Description</font></b></td>\n";
          			table += "<td width=334><b><font size=2 face=Verdana>Notes</font></b></td>\n";
        			table += "</tr>\n";


      			}


      			date = dfDateField.format(rs.getDate("entrydate"));
      			description = rs.getString("Description");
        		notes = rs.getString("Notes");
        		eventname = rs.getString("EventName");
        		eventid = rs.getString("EventID");



      			recordCounter++;
				if (recordCounter % 2 == 1)
				{

      				table += "<tr bgcolor=#eaeaea align=left valign=top>\n";
      			}
      			else
      			{
      				table += "<tr bgcolor=#ffffff align=left valign=top>\n";
      			}





          		table += "<td width=143><font size=2 face=Verdana><A HREF=" + pathURL + "?form=modify&value=" + eventid + ">" + eventname + "</a></font></td>\n";

          		table += "<td width=123><font size=2 face=Verdana>" + description + "</font></td>\n";
          		table += "<td width=334><font size=2 face=Verdana>" + notes + "</font></td>\n";
        		table += "</tr>\n";






  			}
  			if( j == 0 )
			{

			table += "<tr><td><div align=center><br><br><font face=Verdana>No events are scheduled for "+ sdfOutput.format( newDate ) +".</font><br>";
			table += "<font face=Verdana size=1><b><a href=" + pathURL + "?form=new&value="+ date +">Create a new event for this day.</a></b><br><br><br></font></div></td></tr></table>";


        	}



    	}
    	catch (Exception e)
    	{
      	return e.getMessage();
    	}
    	return table ;
    }

 /*************************/
 /* New Event Entry Point */
 /*************************/

  	String newEvent(String date)
  	{
    	StringBuffer returnValue = new StringBuffer();



    	try
    	{

			SimpleDateFormat sdfInput = new SimpleDateFormat( "M/dd/yy" );

                SimpleDateFormat sdfOutput = new SimpleDateFormat ( "EEEE, MMMM dd, yyyy" );
    			String textDate = date;

    			Date newDate = sdfInput.parse( textDate );





    		returnValue.append("<body link=#000000 vlink=#000000 alink=#000000>"
     		+ "<center><b><font face=Verdana>New Event for " + sdfOutput.format( newDate ) + "</font></b><br>"
     		+ "<font face=Verdana size=1><b><a href=" + pathURL + "?date=" + date + ">Calendar View</a> &#8226; "

     		+ "<a href=# onClick=window.print();return false>Print This Page</a></b></font></center><br><br>"




     		+ "<FORM METHOD=POST ACTION='testCalendarServlet'>"
     		+ "<INPUT TYPE=HIDDEN NAME='UserRequest' VALUE='New'>"
     		+ "  <center><TABLE BORDER=0 CELLSPACING=0>"
     		+ "  <TR><TD><font face=Verdana>Date:</TD><TD><INPUT TYPE=TEXT SIZE=8 NAME=EventDate VALUE=" + date + " readonly=true></font></TD></TR>"
     		+ "  <TR><TD><INPUT TYPE=hidden NAME=EventType VALUE=SPECIAL CHECKED></TD></TR>"
     		+ "  <TR><TD><font face=Verdana>Event/Name:</font><br><font face=Verdana size=-2>(15 Char Max)</font></TD><TD align=left valign=top><INPUT TYPE=TEXT SIZE=34 maxlength=15 NAME=EventName VALUE=\"Enter Event or Name\"></font></TD></TR>"

     		+ "  <TR><TD><font face=Verdana>Description:</TD><TD>"

  			+ "<select name=\"Description\" size=\"1\">"
    		+ "<option value=\"Vac\">Vacation</option>"
    		+ "<option value=\"Illness\">Illness</option>"
    		+ "<option value=\"Out\">Out</option>"
    		+ "<option value=\"Meeting\">Meeting</option>"
    		+ "<option value=\"Comp\">Comp</option>"
    		+ "<option value=\"Other\">Other</option>"
  			+ "</select>"
			+ "</font></TD></TR>"
       		+ "  <TR><TD VALIGN=TOP><font face=Verdana>Notes:</font><br><font face=Verdana size=-2>*Frist 2 words<br>will be displayed<br>on the Calendar<br>View</font></TD><TD><TEXTAREA NAME=Notes ROWS=6 COLS=32 WRAP=SOFT></TEXTAREA></font></TD></TR>"
     		+ "  <TR><TD COLSPAN=2>"
     		+ "  <center><INPUT TYPE=SUBMIT VALUE='Enter Event'></center>"
     		+ "  </TD></TR><TR><TD COLSPAN=2>"
     		+ "  </TD></TR>"
     		+ "  </TABLE></center>"
     		+ "</FORM>");


			}

    		catch(Exception e)
			{
				e.printStackTrace();
				System.err.println(e.getMessage());
			}

    	return returnValue.toString();
  	}


 /****************************/
 /* Modify Event Entry Point */
 /****************************/

  	String modifyEvent(String eventID)
  	{
    	StringBuffer returnValue = new StringBuffer();
    	String date = null;
    	String description = null;
    	String notes = null;
    	String eventname = null;



    	//** retrieve the existing data for the selected event
    	try
    	{
      		Class.forName(mySqlJdbcDriver).newInstance();
      		Connection conn = DriverManager.getConnection(wbcalendarDB,mysqlUser,mysqlPassword);
      		Statement stmt = conn.createStatement();
      		String sqlQueryString = "SELECT * FROM wbcalendar WHERE EventID='" + eventID +"'";
      		ResultSet rs = stmt.executeQuery(sqlQueryString);

      		if (rs.next())
      		{


        		date = dfDateField.format(rs.getDate("entrydate"));

        		description = rs.getString("Description");
        		notes = rs.getString("Notes");
        		eventname = rs.getString("EventName");
      		}


      		SimpleDateFormat sdfInput = new SimpleDateFormat( "M/dd/yy" );
			SimpleDateFormat sdfOutput = new SimpleDateFormat ( "EEEE, MMMM dd, yyyy" );
    		String textDate = date;
   			Date newDate = sdfInput.parse( textDate );


      		// print a pre-loaded form for revising an existing event:

    		returnValue.append("<body link=#000000 vlink=#000000 alink=#000000>"
     		+ "<center><b><font face=Verdana>Modify Event for " + sdfOutput.format( newDate ) + "</font></b><br>"
     		+ "<font face=Verdana size=1><b><a href=" + pathURL + "?date=" + date + ">Calendar View</a> &#8226; "
     		+ "<a href=# onClick=window.print();return false>Print This Page</a></b></font></center><br><br>"
	     	+ "<FORM METHOD=POST ACTION='testCalendarServlet'>"
	     	+ "<INPUT TYPE=HIDDEN NAME='UserRequest' VALUE='Modify'>"
	     	+ "<INPUT TYPE=HIDDEN NAME='EventID' VALUE=" + eventID + ">"
	     	+ "<center><TABLE BORDER=0 CELLSPACING=0>"
	     	+ "<TR><TD><font face=Verdana>Date:</font></TD><TD>"

	     	+ "<INPUT TYPE=TEXT SIZE=8 readonly=true NAME='EventDate' VALUE=" + date + "></TD>"
	     	+ "</TR>"
	     	+ "</TD></TR>"

	     	+ "<TR><TD><INPUT TYPE=hidden NAME=EventType VALUE=SPECIAL CHECKED></TD></TR>"

	     	+ "<TR><TD><font face=Verdana>Event/Name:</font><br><font face=Verdana size=-2>(15 Char Max)</font></TD><TD align=left valign=top><INPUT TYPE=TEXT SIZE=34 maxlength=15 NAME=EventName VALUE=\""
	     	+ removeDoubleQuotes(eventname) + "\"></TD></TR>"
	     	+ "<TR><TD><font face=Verdana>Description:</font></TD><TD>"
	     	+ "<select name=\"Description\" size=\"1\">"
	     	+ "<option value=\"" + removeDoubleQuotes(description) + "\" selected> "+ removeDoubleQuotes(description) + "</option>"
	    	+ "<option value=\"Vac\">Vacation</option>"
	    	+ "<option value=\"Illness\">Illness</option>"
	    	+ "<option value=\"Out\">Out</option>"
	    	+ "<option value=\"Meeting\">Meeting</option>"
	    	+ "<option value=\"Comp\">Comp</option>"
	    	+ "<option value=\"Other\">Other</option>"
	  		+ "</select>"
			+ "</TD></TR>"

	     	+ "<TD></TR>"


	     	+ "<TR><TD></TD><TD></TD></TR>"

	     	+ "<TR><TD VALIGN=TOP><font face=Verdana>Notes:</font>"
	     	+ "</FONT><br><font face=Verdana size=-2>*Frist 2 words<br>will be displayed<br>on the Calendar<br>View</font></TD><TD><TEXTAREA NAME=Notes ROWS=6 COLS=32 WRAP=SOFT>"
	     	+ notes + "</TEXTAREA></TD></TR>"
	     	+ "  </TD></TR><TR><TD COLSPAN=2>"
	     	+ "  <center><TABLE BORDER=0 CELLSPACING=0><TR>"
	     	+ "    <TD><INPUT TYPE=SUBMIT VALUE='Modify'></TD>"
	     	+ "    <TD><INPUT TYPE=SUBMIT VALUE='Delete' OnClick=UserRequest.value='Delete'></TD></FORM>"
	     	+ "  </TABLE></center>"
	     	+ "  </TD></TR><TR><TD COLSPAN=2>"

	     	+ "  </TD></TR>"
	     	+ "  </TABLE></center>");


    	}
    	catch (Exception e)
    	{
      	return e.getMessage();
    	}

      	return returnValue.toString();
  	}


 /********************************/
 /* Calendar Changes Entry Point */
 /********************************/


  	public void doPost(HttpServletRequest req,HttpServletResponse res)
    	throws ServletException, IOException
    {

    	res.setContentType("text/html");
    	PrintWriter out = res.getWriter();
    	pathURL = req.getServletPath();



    	String userRequest = req.getParameter("UserRequest");
    	String eventID = req.getParameter("EventID");
    	String sqlQuery = null;


    	if (userRequest.equals("Delete"))
    	{

      		String entrydate = null;
      		String convertedDate = null;

      		try
      		{

      			String eventType = req.getParameter("EventType");
      			String date = dfMySQLDate.format(dfDateField.parse(req.getParameter("EventDate")));

        		//This is used to convert mysql date format from YYYY-MM-DD to be M/dd/yy
        		String convertDateFormat = dateFormat.format(dfDateField.parse(req.getParameter("EventDate")));
        		convertedDate = convertDateFormat;

      			sqlQuery = "DELETE FROM wbcalendar WHERE EventID=" + eventID;
      			res.sendRedirect(""+ pathURL + "?date=" + convertedDate + "");
      		}
      		catch (Exception e)
      		{

        		out.println("</body></html>");
        		return;
      		}

    	}
    	else
    	{
      		String entrydate = null;
      		String convertedDate = null;
      		String description = removeSingleQuotes(req.getParameter("Description"));
      		String eventname = removeSingleQuotes(req.getParameter("EventName"));
      		String notes = removeSingleQuotes(req.getParameter("Notes"));

      		try
      		{
        		String eventType = req.getParameter("EventType");
        		String date = dfMySQLDate.format(dfDateField.parse(req.getParameter("EventDate")));

        		//This is used to convert mysql date format from YYYY-MM-DD to be M/dd/yy
        		String convertDateFormat = dateFormat.format(dfDateField.parse(req.getParameter("EventDate")));

        		if (eventType.equals("SPECIAL"))
        		{
          			entrydate = date;

          			convertedDate = convertDateFormat;

        		}

      		}
      		catch (Exception e)
      		{

        		out.println("</body></html>");
        		return;
      		}

      		if (userRequest.equals("New"))
      		{


        		sqlQuery = "INSERT INTO wbcalendar (EntryDate,EventName,Description,Notes) VALUES "

         		+ "('" + entrydate + "','" + eventname + "','"
         		+ description + "','" + notes + "')";

            System.out.println(sqlQuery);

         		// Could use variable instead of hard coding the url path
         		//res.sendRedirect(""+ pathURL);

         		// User redirected back to current month after entry is added
         		//res.sendRedirect("http://localhost/servlet/WBCalendar");

         		// User redirected to month they added entry to
         		res.sendRedirect("" + pathURL + "?date=" + convertedDate + "");
      		}
      		else if (userRequest.equals("Modify"))
      		{

        		sqlQuery = "UPDATE wbcalendar SET "

         		+ "EntryDate='" + entrydate
         		+ "',EventName='" + eventname + "',Description='" + description + "',Notes='" + notes
         		+ "' WHERE EventID=" + eventID;

         		res.sendRedirect("" + pathURL + "?date=" + convertedDate + "");
      		}
    	}

    	try
    	{
      		Class.forName(mySqlJdbcDriver).newInstance();
      		Connection conn = DriverManager.getConnection(wbcalendarDB,mysqlUser,mysqlPassword);
      		Statement stmt = conn.createStatement();

      		if (userRequest.equals("Delete"))
      		{
            // userRequest.equals("Delete")
        		if (stmt.executeUpdate(sqlQuery) != 1)
          		out.println("Caution: possible database error " + stmt.executeUpdate(sqlQuery));

       			out.println("<body>");

      		}
      		else
      		{
      			  stmt.executeUpdate(sqlQuery);
      		}
      		out.println("</body></html>");
      		stmt.close();
      		conn.close();
    	}
    	catch (Exception e)
    	{
    		// SqlExceptions caught here
      		out.println("<body onLoad=finish('dbError','" + req.getParameter("EventDate") + "');>");
      		System.out.println(e.getMessage());
      		out.println("</body></html>");
    	}
  	}



  	String removeSingleQuotes(String inString)
  	{
    	int i = inString.indexOf('\'',0);
    	return i<0?inString:removeSingleQuotes(new StringBuffer(inString).insert(i,'\\').toString(),i+2);
  	}

  	String removeSingleQuotes(String inString,int fromIndex)
  	{
    	int i = inString.indexOf('\'',fromIndex);
    	return i<0?inString:removeSingleQuotes(new StringBuffer(inString).insert(i,'\\').toString(),i+2);
  	}

  	String removeDoubleQuotes(String inString)
  	{
    	int i = inString.indexOf('\"',0);
    	return i<0?inString:removeDoubleQuotes(new StringBuffer(inString).replace(i,i+1,"&quot;").toString(),i+5);
  	}

  	String removeDoubleQuotes(String inString,int fromIndex)
  	{
    	int i = inString.indexOf('\"',fromIndex);
    	return i<0?inString:removeDoubleQuotes(new StringBuffer(inString).replace(i,i+1,"&quot;").toString(),i+5);
  	}
}